import datetime
import decimal
from comment.utils.data2dict import BaseListSerializer, BaseSerializer, BasePaginateSerializer

'''
    首页数据的序列化
'''

class ProductSerializer(BaseListSerializer):
    """商品的详情序列化"""

    def to_dict(self):
        list = []
        for obj in self.data_list:
            list.append(
                {
                    'id': obj.id,
                    "product_name": obj.product_name,
                    "defaultPic": obj.default_pic,
                    "price": float(obj.price)
                }
            )
        return list


class CategorySerializer(BaseListSerializer):
    """商品分类序列化"""

    def to_dict(self):
        bean_list = self.data_list
        for bean in bean_list:
            for key in bean.keys():
                if isinstance(bean[key], datetime.datetime) or isinstance(bean[key], datetime.date):
                    bean[key] = str(bean[key])
                if isinstance(bean[key], decimal.Decimal):
                    bean[key] = float(bean[key])

        return bean_list


class RecommendSerializer(BaseSerializer):
    """推荐专题序列化"""

    def to_dict(self):
        obj = self.data
        return {
                'id': obj.id,
                "subjectName": obj.title,
                # 专题热词 分割成 list
                "hotWordList": obj.hot_words.split(','),
                "pic": obj.pic,
                # 画册图片分割
                "albumPicList": obj.album_pics.split(',')
                }


class GoodslistPaginateSerializer(BasePaginateSerializer):
    """ 商品列表分页 序列化"""

    def get_object(self, obj):
        """暂时就返回2条字段,具体再增加"""
        return {
            'id': obj.id,
            'productName': obj.product_name,
            'defaultPic': obj.default_pic,
            'relCategory3Id': obj.rel_category3_id,
            'price': float(obj.price),
            'salesNum': obj.sales_num
        }





